<template>
<div class="order-box">
  <!--面包屑部分-->
  <el-breadcrumb separator="/">
    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item :to="{ path: '/orders' }">订单管理</el-breadcrumb-item>
    <el-breadcrumb-item>订单列表</el-breadcrumb-item>
  </el-breadcrumb>
  <el-card>
    <!--订单搜索-->
    <el-row>
      <el-col :span="6">
        <el-input placeholder="请输入内容" v-model="queryInfo.query" class="input-with-select"  clearable @clear="getOrderList">
          <el-button slot="append" icon="el-icon-search" @click="getOrderList"></el-button>
        </el-input>
      </el-col>
    </el-row>
    <el-row>
      <el-col>
        <el-table
          :data="orderList"
          border
          style="width: 100%">
          <el-table-column
            label="#"
            type="index"
            align="center"
            width="50">
          </el-table-column>
          <el-table-column
            prop="order_number"
            label="订单编号"
            align="center"
            >
          </el-table-column>
          <el-table-column
            prop="order_price"
            label="订单价格"
            align="center"
            width="100">
          </el-table-column>
          <el-table-column
            prop="order_pay"
            label="是否付款"
            align="center"
            width="100">
            <template v-slot="scope">
              <el-tag type="danger" v-if="scope.row.order_pay == 0">未付款</el-tag>
              <el-tag type="success" v-else>已付款</el-tag>
            </template>
          </el-table-column>
          <el-table-column
            prop="is_send"
            label="是否发货"
            align="center"
            width="100">
          </el-table-column>
          <el-table-column
            prop="create_time"
            label="下单时间"
            align="center"
            width="180">
            <template v-slot="scope">
              {{scope.row.create_time | dateFilter}}
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            width="250">
            <template v-slot="scope">
              <!--编辑订单信息-->
              <el-button type="primary" icon="el-icon-edit" size="medium"
                         @click="editOrder(scope.row.order_number)"></el-button>
              <!--查看订单信息-->
              <el-button type="primary" icon="el-icon-view" size="medium"
                         @click="viewOrder(scope.row.order_number)"></el-button>
              <!--查看物流信息-->
              <el-button type="primary" icon="el-icon-location" size="medium"
                         @click="viewKuaidi(scope.row.order_number)"></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!--分页-->
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pagenum"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="queryInfo.pagesize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total">
        </el-pagination>
      </el-col>
    </el-row>
  </el-card>
  <!--修改订单地址对话框-->
  <el-dialog
    title="修改订单地址"
    :visible.sync="updateOrderAddressdialogVisible"
    @close="updateOrderAddressdialogClose"
    :close-on-click-modal="false"
    width="40%"
    >
    <div class="order-address-box">
      <el-form :model="orderAddressInfo"
               :rules="orderAddressInfoRules"
               :close-on-click-modal="false"
               ref="orderAddressInfoRef"
               label-width="120px"
      >
        <el-form-item label="省市区/县" prop="city">
          <el-cascader
            v-model="selectId"
            :options="cityData"
            :props="orderAddressInfoProps"
            @change="citySelectChange"
            clearable
          ></el-cascader>
        </el-form-item>
        <el-form-item label="详细地址" prop="addr">
          <el-input v-model="orderAddressInfo.addr"></el-input>
        </el-form-item>
      </el-form>
    </div>
    <span slot="footer" class="dialog-footer">
    <el-button @click="updateOrderAddressdialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="confirmUpdateAddress">确 定</el-button>
  </span>
  </el-dialog>

  <!--查看订单物流信息对话框-->
  <el-dialog
    title="查看订单物流信息"
    :visible.sync="viewProgressdialogVisible"
    :close-on-click-modal="false"
    width="40%"
  >
    <div class="order-address-box">
      <el-radio-group v-model="reverse">
        <el-radio :label="true">正序</el-radio>
        <el-radio :label="false">倒序</el-radio>
      </el-radio-group>
      <el-timeline :reverse="reverse">
        <el-timeline-item
          v-for="(activity, index) in progressInfo.activities"
          :key="index"
          :timestamp="activity.time">
          {{activity.context}}
        </el-timeline-item>
      </el-timeline>
      </div>
    <span slot="footer" class="dialog-footer">
    <el-button @click="viewProgressdialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="viewProgressdialogVisible = false">确 定</el-button>
  </span>
  </el-dialog>
</div>
</template>

<script>
import cityData from './citydata'
export default {
  name: 'order',
  data(){
    return{
      // 订单数据
      orderList:[],
      // 查询条件
      queryInfo:{
        // 查询参数
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 10,
        // 用户 ID
        // user_id:'',
        // // 支付状态
        // pay_status:'',
        // // 是否发货
        // is_send:'',
        // // ['个人','公司']
        // order_fapiao_title:'',
        // // 公司名称
        // order_fapiao_company:'',
        // // 发票内容
        // order_fapiao_content:'',
        // // 发货地址
        // consignee_addr:'',
      },
      // 订单总数
      total: 0,
      // 将省市区文件数据页面绑定
      cityData: cityData,
      // 修改订单地址信息对话框显示控制
      updateOrderAddressdialogVisible: false,
      // 订单地址信息
      orderAddressInfo:{
        city:'',
        addr:''
      },
      // 省市区信息
      ctiyList:[],
      // 修改订单地址信息规则
      orderAddressInfoRules: {
        city: [
          {required: true, message: '请选择省市信息', trigger: 'blur'},
        ],
        addr: [
          {required: true, message: '请输入详细地址信息', trigger: 'blur'},
        ],
      },
      // 级联选择器属性配置
      orderAddressInfoProps:{
        label: 'label',
        value: 'value',
        children: 'children',
        checkStrictly: true,
        expandTrigger: 'hover'
      },
      // 选择省市区
      selectId:[],
      // 物流信息
      progressInfo:{
        activities: [
          {
            "time": "2018-05-10 09:39:00",
            "ftime": "2018-05-10 09:39:00",
            "context": "已签收,感谢使用顺丰,期待再次为您服务",
            "location": ""
          },
          {
            "time": "2018-05-10 08:23:00",
            "ftime": "2018-05-10 08:23:00",
            "context": "[北京市]北京海淀育新小区营业点派件员 顺丰速运 95338正在为您派件",
            "location": ""
          },
          {
            "time": "2018-05-10 07:32:00",
            "ftime": "2018-05-10 07:32:00",
            "context": "快件到达 [北京海淀育新小区营业点]",
            "location": ""
          },
          {
            "time": "2018-05-10 02:03:00",
            "ftime": "2018-05-10 02:03:00",
            "context": "快件在[北京顺义集散中心]已装车,准备发往 [北京海淀育新小区营业点]",
            "location": ""
          },
          {
            "time": "2018-05-09 23:05:00",
            "ftime": "2018-05-09 23:05:00",
            "context": "快件到达 [北京顺义集散中心]",
            "location": ""
          },
          {
            "time": "2018-05-09 21:21:00",
            "ftime": "2018-05-09 21:21:00",
            "context": "快件在[北京宝胜营业点]已装车,准备发往 [北京顺义集散中心]",
            "location": ""
          },
          {
            "time": "2018-05-09 13:07:00",
            "ftime": "2018-05-09 13:07:00",
            "context": "顺丰速运 已收取快件",
            "location": ""
          },
          {
            "time": "2018-05-09 12:25:03",
            "ftime": "2018-05-09 12:25:03",
            "context": "卖家发货",
            "location": ""
          },
          {
            "time": "2018-05-09 12:22:24",
            "ftime": "2018-05-09 12:22:24",
            "context": "您的订单将由HLA（北京海淀区清河中街店）门店安排发货。",
            "location": ""
          },
          {
            "time": "2018-05-08 21:36:04",
            "ftime": "2018-05-08 21:36:04",
            "context": "商品已经下单",
            "location": ""
          }
        ],
        "meta": { "status": 200, "message": "获取物流信息成功！" }
      },
      // 查看物流信息对话框
      viewProgressdialogVisible: false,
      // 物流进度倒序
      reverse: false,
    }
  },
  created() {
    this.getOrderList()
  },
  methods:{
    // 获取订单列表
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', {
        params: this.queryInfo
      })
      if (res.meta.status !== 200) {
        return this.$message.error('获取订单列表数据失败!')
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 订单数据分页-每页显示条数
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.queryInfo.pagesize = val
      this.getOrderList()
    },
    // 订单数据分页-每页显示条数
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.queryInfo.pagenum = val
      this.getOrderList()
    },
    // 编辑订单地址信息
    editOrder(id){
      this.updateOrderAddressdialogVisible = true
    },
    // 查看订单信息
    async viewOrder(id){
      console.log(`当前订单id: ${id}`)
      const {data: res} = await this.$http.get(`orders/${id}`)
      if(res.meta.status !== 200) return this.$message.error('获取订单信息失败！')
      this.$message.success('获取订单信息成功！')
    },
    // 查看物流信息
    async viewKuaidi(id){
      this.viewProgressdialogVisible=true
      // const {data: res} = await this.$http.get(`/kuaidi/1106975712662`)
      // console.log('物流信息', res.data)
    },
    // 级联选择器选择省市区数据
    citySelectChange(){
      console.log('级联选择器事件', this.selectId)
    },
    // 修改订单地址信息对话框关闭事件
    updateOrderAddressdialogClose(){
      // 重置表单
      this.$refs.orderAddressInfoRef.resetFields()
      // 重置级联选择器
      this.selectId= []
    },
    // 提交修改地址信息
    confirmUpdateAddress(){
      // 修改地址信息数据
      const addressInfo= {
        city: this.selectId.join(','),
        addr: this.orderAddressInfo.addr
      }
      console.log('修改地址信息数据',addressInfo)
      // 发起网络请求
      // 修改成功，隐藏对话框
      this.updateOrderAddressdialogVisible = false
      // 修改成功，重新请求订单信息
      this.getOrderList()
      // 修改成功，提示成功
      this.$message.success('修改地址成功！')
    },
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-step__description{
  padding: 10px 5px;
}
.order-address-box .el-radio-group{
  padding: 10px 0;
}
</style>
